<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserWenjuan;
use App\Admin\Repositories\Config;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Checkbox;

class UserWenjuanController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserWenjuan('user'), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('user.name', '用户');
            $grid->column('fu_check_date');
            $grid->column('created_at');
            if(!\Dcat\Admin\Admin::user()->isAdministrator()) {
                $grid->model()->where('user.shop_id', session('shop_id'));
            }
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('user.name', '姓名')->width(3);
                $filter->like('user.mobile', '手机号')->width(3);
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserWenjuan(), function (Show $show) {
            $model = $show->model();
            $show->html(view('admin.show.wenjuan', compact('model')));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form($id = null)
    {
        Admin::script(
                    <<<JS
var aoye1 = $('input[name="shenghuo[aoye1]"]').parents('.form-group');
var aoye2 = $('input[name="shenghuo[aoye2]"]').parents('.form-group');
(function () {
    var user_rewrite_refresh = (function (val){
        if (val==485){
            aoye1.show();
            aoye2.hide();
        }else if (val==486){
            aoye1.hide();
            aoye2.show();
        }  else {
            aoye1.hide();
            aoye2.hide();
        }
    });
    $('input[name="shenghuo[aoye]"]').on('click', function () {
        user_rewrite_refresh($(this).val())
    });

    $('.has-many-table-fudao_kewai').parents('.form-group').find('.col-md-2').remove();
    $('.has-many-table-fudao_kewai').parents('.form-group').find('.col-md-8').addClass('col-md-12');
    
})();
JS
                );




        return Form::make(new UserWenjuan(), function (Form $form) use ($id) {
            if($id == -1) {
                $form->disableFooter();
                $form->disableHeader();
            }
            $model = $form->model();
            $aoye = 0;
            if($model) {
                $aoye = \Illuminate\Support\Arr::get($model->shenghuo, 'aoye', 0);
            }
            Admin::script(
                    <<<JS
if('{$aoye}' == 485){
    aoye1.show();
    aoye2.hide();
}else if('{$aoye}' == 486){
    aoye1.hide();
    aoye2.show();
}else{
    aoye1.hide();
    aoye2.hide();
}
JS
                );
        if($id != -1) {
            $form->column(6, function ($form) {
                $form->select('user_id', '用户')->options(function ($id) {
                            $user = \App\Models\User::find($id);
                            if ($user) {
                                    return [$user->id => $user->name];
                                }
                })->ajax(admin_url('api/user'))->required();
            });
            $form->column(6, function ($form) {
                $form->date('write_date')->default(date('Y-m-d'));

            });
        }else{
            $form->column(12, function ($form) {
                $form->date('write_date')->default(date('Y-m-d'));

            });
        }
            
            $form->column(6, function ($form) {
                $form->embeds('zhenduan', ' ', function ($form) {
                        $form->dividetitle('1、初步分类');
                        $form->checkbox('zhenduan1', '')->options(Config::selectOptions(pid: 425))->setFieldClass('col-md-10', true);
                        $form->text('other', '其他')->setFieldClass('col-md-10', true);

                        $form->checkbox('xieshi', '斜视')->options(Config::selectOptions(pid: 433))->setFieldClass('col-md-10', true);

                        $form->checkbox('tiaojie', '调节功能异常')->options(Config::selectOptions(pid: 477))->setFieldClass('col-md-10', true);
                        
                });

            });
            $form->column(6, function ($form) {
                $form->embeds('huyan', '', function ($form) {
                    $form->dividetitle('2、现有护眼措施');    
                    $form->checkbox('huyan1', '')->options(Config::selectOptions(pid: 515))->setFieldClass('col-md-10', true);
                    $form->text('other', '其他')->setFieldClass('col-md-10', true);
                })->setFieldClass('col-md-10', true);
            });
            $form->column(6, function ($form) {
                $form->embeds('shenghuo', '', function ($form) {
                    $form->dividetitle('3、睡眠习惯');
                        $form->daterangeformat('shangxue', 'shangxue1', '上学期间')->format('HH:mm')->help('入睡 ⏰ [__:__] | 起床 ⏰ [__:__] ')->setFieldClass('col-md-10', true);
                        $form->daterangeformat('fangjia', 'fangjia1', '放假期间')->format('HH:mm')->help('入睡 ⏰ [__:__] | 起床 ⏰ [__:__] ')->setFieldClass('col-md-10', true);
                        
                        $form->radio('aoye', '熬夜频率')->options(Config::selectOptions(pid: 483))->setFieldClass('col-md-10', true);
                        $form->text('aoye1', '近3月几次')->setFieldClass('col-md-10', true);
                        $form->daterangeformat('aoye2', '最晚几点入睡')->format('HH:mm')->setFieldClass('col-md-10', true);
                        
                        $form->divider('户外活动')->setFieldClass('col-md-10', true);
                        $form->daterangeformat('shangwu', 'shangwu1', '上午')->format('HH:mm')->help('上午 [__:__至__:__]')->setFieldClass('col-md-10', true);
                        $form->dateRangeFormat('shangwu', 'shangwu1', '上午')->format('HH:mm')->help('上午 [__:__至__:__]')->setFieldClass('col-md-10', true);
                        $form->daterangeformat('xiawu', 'xiawu1', '下午')->format('HH:mm')->help('下午 [__:__至__:__]  ')->setFieldClass('col-md-10', true);
                        $form->checkbox('peitong', '陪同者')->options(Config::selectOptions(pid: 487))->setFieldClass('col-md-10', true);
                        $form->text('huodongnei', '活动内容')->setFieldClass('col-md-10', true);

                        $form->divider('作业习惯');
                        $form->text('rijunshichang', '日均时长')->help('⏰ [__]小时')->setFieldClass('col-md-10', true);
                        $form->text('dancilianxu', '单次连续时长')->help('⏰ [__]分钟')->setFieldClass('col-md-10', true);
                        $form->checkbox('xiuxifangshi', '休息方式')->options(Config::selectOptions(pid: 492))->setFieldClass('col-md-10', true);
                        $form->text('xiuxifangshi_other', '其他休息方式')->setFieldClass('col-md-10', true);
                        $form->checkbox('fuzhugongju', '辅助工具')->options(Config::selectOptions(pid: 497))->setFieldClass('col-md-10', true);
                        $form->checkbox('peitong', '陪同者')->options(Config::selectOptions(pid: 501))->setFieldClass('col-md-10', true);
                        $form->checkbox('zhaoming', '照明')->options(Config::selectOptions(pid: 504))->setFieldClass('col-md-10', true);

                })->setFieldClass('col-md-10', true);

                

            });
            $form->column(6, function ($form) {
                $form->embeds('dianzi', '', function ($form) {
                        $form->dividetitle('4、电子设备使用');
                        $form->text('dianshi', '看电视')->help('分钟/天')->setFieldClass('col-md-10', true);
                        $form->text('dianshi_neirong', '内容')->help('看电视内容')->setFieldClass('col-md-10', true);
                        $form->checkbox('dianshi_zishi', '')->options(Config::selectOptions(pid: 508))->setFieldClass('col-md-10', true);
                        $form->text('dianshi_mi', '')->help('米')->setFieldClass('col-md-10', true);
                        $form->text('shouji_neirong', '内容')->help('看手机内容')->setFieldClass('col-md-10', true);
                        $form->text('shouji', '手机/PAD时间')->help('分钟/天')->setFieldClass('col-md-10', true);
                        $form->checkbox('shouji_zishi', '')->options(Config::selectOptions(pid: 508))->setFieldClass('col-md-10', true);
                        $form->text('shouji_mi', '')->help('米')->setFieldClass('col-md-10', true);
                        $form->text('shouji_xiuxi', '')->help('分钟/休息次')->setFieldClass('col-md-10', true);

                    });
                    $form->embeds('jiazhang', '', function ($form) {
                        $form->dividetitle('5、家长核心诉求');    
                        $form->checkbox('hexin', '')->options(Config::selectOptions(pid: 527))->setFieldClass('col-md-10', true);
                        $form->text('other', '其他')->setFieldClass('col-md-10', true);

                    });
                    
                    $form->date('fu_check_date');
                    

            });
            $form->column(12, function (Form $form) {
                $form->divider('课外班情况');
                $form->checkbox('fudao_leixing', '类型')->options(Config::selectOptions(pid: 512));
                $form->table('fudao_kewai', '', function (Form\NestedForm $table) {
                        $table->text('zhouji', '周几');
                        $table->date('shangke', '几点')->format('HH:mm');
                        $table->number('shijian', '时间')->help('小时');
                });
            });

            
            // $form->saving(function(Form $form){
            //     dd($form->huyan);
            // });
            
           
        });

        
                    
    }
}
